package com.laoniu.controller;

import com.laoniu.annotation.OperateLog;
import com.laoniu.biz.UserBiz;
import com.laoniu.dto.LoginUser;
import com.laoniu.dto.RestResult;
import com.laoniu.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("admin")
public class LoginController {


    @Autowired
    private UserBiz userBiz;
    @Autowired
    private PasswordEncoder bcryptPasswordEncoder;
    /**
     * 登录
     * @return
     */
    @PostMapping("login")
    public Map<String,Object> login(@RequestBody LoginUser userDto){
        return userBiz.loginUser(userDto);
    }

    @GetMapping("getPassword")
    public String getPassword(@RequestParam String password){
        ServletUtils.getDevice();
        String requestIp = ServletUtils.getUrl();
        System.out.println(requestIp);
        return bcryptPasswordEncoder.encode(password);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("logout")
    @OperateLog(bizName = "退出登录", operateDesc = "登出系统", operateType = "登出")
    public RestResult logout(){
        userBiz.logout();
        return RestResult.success();
    }
}
